package com.ruoyi.web.controller.conference;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.BooleanUtils;
import com.ruoyi.common.core.page.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.conference.domain.MeetingRoomManage;
import com.ruoyi.conference.service.IMeetingRoomManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会议管理Controller
 * 
 * @author ruoyi
 * @date 2023-03-04
 */
@RestController
@RequestMapping("/conference/manage")
public class MeetingRoomManageController extends BaseController
{
    @Autowired
    private IMeetingRoomManageService meetingRoomManageService;

    // 缓存需要回滚的信息  TODO 更改为redis缓存技术
    static R r = new R();

    /**
     * 查询会议管理列表
     */
    @PreAuthorize("@ss.hasPermi('conference:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeetingRoomManage meetingRoomManage)
    {
        startPage();
        List<MeetingRoomManage> list = meetingRoomManageService.selectMeetingRoomManageList(meetingRoomManage);
        return getDataTable(list);
    }

    /**
     * 查询会议记录列表
     */
    @PreAuthorize("@ss.hasPermi('record:meetingMinutes:list')")
    @GetMapping("/recordList")
    public TableDataInfo recordList(MeetingRoomManage meetingRoomManage)
    {
        startPage();
        List<MeetingRoomManage> list = meetingRoomManageService.selectMeetingRoomManageRecordList(meetingRoomManage);
        return getDataTable(list);
    }

    /**
     * 导出会议管理列表
     */
    @PreAuthorize("@ss.hasPermi('conference:manage:export')")
    @Log(title = "会议管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeetingRoomManage meetingRoomManage)
    {
        List<MeetingRoomManage> list = meetingRoomManageService.selectMeetingRoomManageList(meetingRoomManage);
        ExcelUtil<MeetingRoomManage> util = new ExcelUtil<MeetingRoomManage>(MeetingRoomManage.class);
        util.exportExcel(response, list, "会议管理数据");
    }

    /**
     * 导出会议管理列表
     */
    @PreAuthorize("@ss.hasPermi('record:meetingMinutes:export')")
    @Log(title = "会议记录", businessType = BusinessType.EXPORT)
    @PostMapping("/Record/export")
    public void exportRecord(HttpServletResponse response, MeetingRoomManage meetingRoomManage)
    {
        List<MeetingRoomManage> list = meetingRoomManageService.selectMeetingRoomManageRecordList(meetingRoomManage);
        ExcelUtil<MeetingRoomManage> util = new ExcelUtil<MeetingRoomManage>(MeetingRoomManage.class);
        util.exportExcel(response, list, "会议记录数据");
    }

    /**
     * 获取会议管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('conference:manage:query')")
    @GetMapping(value = "/{mrmId}")
    public AjaxResult getInfo(@PathVariable("mrmId") Integer mrmId)
    {
        return success(meetingRoomManageService.selectMeetingRoomManageByMrmId(mrmId));
    }

    /**
     * 新增会议管理
     */
    @PreAuthorize("@ss.hasPermi('conference:manage:add')")
    @Log(title = "会议管理", businessType = BusinessType.INSERT)
    @PostMapping
    public BooleanUtils add(@RequestBody MeetingRoomManage meetingRoomManage)
    {
//        return toAjax(meetingRoomManageService.insertMeetingRoomManage(meetingRoomManage));
        return meetingRoomManageService.insertMeetingRoomManage(meetingRoomManage);
    }


    /**
     * 修改会议管理
     */
    @PreAuthorize("@ss.hasPermi('conference:manage:edit')")
    @Log(title = "会议管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public BooleanUtils edit(@RequestBody MeetingRoomManage meetingRoomManage)
    {
        return meetingRoomManageService.updateMeetingRoomManage(meetingRoomManage);
    }

    /**
     * 删除会议管理
     */
    @PreAuthorize("@ss.hasPermi('conference:manage:remove')")
    @Log(title = "会议管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mrmIds}")
    public Boolean remove(@PathVariable Integer[] mrmIds)
    {
        return meetingRoomManageService.deleteMeetingRoomManageByMrmIds(mrmIds);
    }

    /**
     * 取消过夜
     */
    @DeleteMapping("/no/{mrmId}")
    public Boolean removeSubscribe(@PathVariable("mrmId") Integer mrmId)
    {
        return meetingRoomManageService.removeSubscribe(mrmId);
    }

    /**
     * 删除会议记录
     */
    @PreAuthorize("@ss.hasPermi('record:meetingMinutes:remove')")
    @Log(title = "会议管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/record/{mrmIds}")
    public AjaxResult removeRecord(@PathVariable Integer[] mrmIds)
    {
        for (Integer mrmId : mrmIds) {
            System.out.println(mrmId);
            r.addList(mrmId);
        }
        return toAjax(meetingRoomManageService.deleteMeetingRoomManageRecordByMrmIds(mrmIds));
    }

    /**
     * 回滚
     */
    @PreAuthorize("@ss.hasPermi('personnel:meetingRoomManage:rollback')")
    @Log(title = "会议记录回滚", businessType = BusinessType.DELETE)
    @DeleteMapping
    public Boolean rollback()
    {
        List list = r.getList();

        if (list.size()==0){
            return false;
        }else {
            Object[] array = list.toArray();
            meetingRoomManageService.rollbackEmployee(array);
            r.setList(new ArrayList<>());
            return true;
        }
    }
}
